/**
 * 
 */
package systole.view.crud.tableModels;

import java.util.Date;
import java.util.List;

import systole.domain.persons.Patient;
import systole.domain.persons.identityCard.IdentityCardType;

/**
 * @author jmj
 * 
 */
public class PatientTableModel extends SystoleTableModel<Patient> {

    private String[] columnNames = {"Apellido", "Nombre", "Tipo Documento",
        "Documento", "Fecha Nacimiento"};
    private static final int NAME_INDEX = 1;
    private static final int SURNAME_INDEX = 0;
    private static final int KIND_IDENTITY_INDEX = 2;
    private static final int IDENTITY_NUMBER_INDEX = 3;
    private static final int BIRTHDATE_INDEX = 4;

    /**
     * @param patients
     */
    public PatientTableModel(List<Patient> patients) {
        super(patients);
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.swing.table.TableModel#getColumnClass(int)
     */
    @Override
    public Class<?> getColumnClass(int column) {
        switch (column) {
            case BIRTHDATE_INDEX:
                return Date.class;
            case IDENTITY_NUMBER_INDEX:
                return Integer.class;
            case KIND_IDENTITY_INDEX:
                return IdentityCardType.class;
            case NAME_INDEX:
                return String.class;
            case SURNAME_INDEX:
                return String.class;
        }
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    @Override
    public int getColumnCount() {
        return this.columnNames.length;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.swing.table.TableModel#getColumnName(int)
     */
    @Override
    public String getColumnName(int column) {
        return this.columnNames[column];
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    @Override
    public Object getValueAt(int row, int column) {
        switch (column) {
            case BIRTHDATE_INDEX:
                return this.entities.get(row).getBirthdate();
            case IDENTITY_NUMBER_INDEX:
                return ((this.entities.get(row).getIdentityCard() != null) ? this.entities.get(row).getIdentityCard().getNumber() : null);
            case KIND_IDENTITY_INDEX:
                return ((this.entities.get(row).getIdentityCard() != null) ? this.entities.get(row).getIdentityCard().getCardType() : null);
            case NAME_INDEX:
                return this.entities.get(row).getName();
            case SURNAME_INDEX:
                return this.entities.get(row).getSurname();
        }
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
     */
    @Override
    public void setValueAt(Object value, int row, int column) {
        switch (column) {
            case BIRTHDATE_INDEX:
                this.entities.get(row).setBirthdate((Date) value);
                break;
            case IDENTITY_NUMBER_INDEX:
                if (this.entities.get(row).getIdentityCard() != null) {
                    this.entities.get(row).getIdentityCard().setNumber((Integer) value);
                }
                break;
            case KIND_IDENTITY_INDEX:
                if (this.entities.get(row).getIdentityCard() != null) {
                    this.entities.get(row).getIdentityCard().setCardType((IdentityCardType) value);
                }
                break;
            case NAME_INDEX:
                this.entities.get(row).setName((String) value);
                break;
            case SURNAME_INDEX:
                this.entities.get(row).setSurname((String) value);
                break;
        }

    }
}
